package net.zjitc.android2019.ch3storage;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;

/**
 * 文件外部存储
 */
public class FileSaveQQ {
    public static boolean save(Context context, String name, String pwd) {
        String device=Environment.getExternalStorageState();
        if(device.equals(Environment.MEDIA_MOUNTED)){
            File path=Environment.getExternalStorageDirectory();
            File file=new File(path,"qq2.txt");
            FileOutputStream out=null;
            BufferedWriter bw=null;
            try {
                out=new FileOutputStream(file);
                bw=new BufferedWriter(new OutputStreamWriter(out));
                bw.write(name+":"+pwd);
                bw.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }finally {
                /*关闭流*/
            }
        }
        return false;
    }

    public static HashMap<String, String> get(Context context) {
        HashMap<String,String> hashMap=new HashMap<String,String>();
        String device=Environment.getExternalStorageState();
        if(device.equals(Environment.MEDIA_MOUNTED)){
            File path=Environment.getExternalStorageDirectory();
            File file=new File(path,"qq2.txt");
            FileInputStream in=null;
            BufferedReader br=null;
            try {
                in=new FileInputStream(file);
                br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                while ((line=br.readLine())!=null){
                    String[] s=line.split(":");
                    hashMap.put("name",s[0]);
                    hashMap.put("pwd",s[1]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                /*关闭流*/
            }

        }
        return hashMap;
    }
}
